<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
* Name:  Selections Model
*
* Author: John Schop, john.schop@gmail.com
*
*/

class Selections_model extends CI_Model
{
	

	public function __construct()
	{
		parent::__construct();
		$this->load->database();
	}
	

	/**
	 * Get all Selections
	 *
	 * @access public
	 * @return object
	 */
	function get_all()
	{
		$this->db->join('users', 'users.id = selections.user_id');
		return $this->db->get('selections')->result();
	}


  /**
   * Get all Selections for active users
   *
   * @access public
   * @return object
   */
  function get_all_active()
  {
    
    $this->db->join('users', 'users.id = selections.user_id');
    $this->db->where('users.active', 1);
    $all = $this->db->get('selections')->result();
    foreach ($all as $user)
    {
      $selection[$user->id] = explode(',', $user->selection);
    }  
    return $selection;
  }  

    /**
     * Get Selection by id
     *
     * @access public
     * @param string $selection_id
     * @return object result object
     */
    function get_by_id($selection_id)
    {
        return $this->db->get_where('selections', array('selection_id' => $selection_id))->row();
    }

    /**
     * Get Selection by user_id
     *
     * @access public
     * @param string $user_id
     * @return object result object
     */
    function get_by_user_id($user_id)
    {
        return $this->db->get_where('selections', array('user_id' => $user_id))->row();
    }


    // This function returns an array of Rider objects
    function get_by_user($user_id)
    {
      $sel_string = $this->db->get_where('selections', array('user_id' => $user_id))->row();

      if(is_object($sel_string))
      {
        $selection = explode(',', $sel_string->selection);

        if(is_array($selection))
        {
          $this->load->model('riders_model');

          foreach($selection as $rider_id)
          {
            $selection_riders[] = $this->riders_model->get_by_id($rider_id);
          }       
        }
      }
      else
      {
        $selection_riders[] = 0;
      }
      return $selection_riders;
    }

    function get_selection_only($user_id)
    {
     
      return $this->db->get_where('selections', array('user_id' => $user_id))->row();
    }
  

    // This function returns an array of Rider objects
    function get_replacements_by_user($user_id)
    {
      $sel_string = $this->db->get_where('selections', array('user_id' => $user_id))->row();
      if(is_object($sel_string))
      {
        $replacements = explode(',', $sel_string->replacements);
        $this->load->model('riders_model');

        foreach($replacements as $rider_id)
        {
          $replacement_riders[] = $this->riders_model->get_by_id($rider_id);
        }       
      }
      else
      {
        $replacement_riders[] = 0;
      }
      return $replacement_riders;
    }

    function erase($user_id)
    {
      $this->update($user_id, array('user_id'=>$user_id, 'selection'=>"", 'replacements'=>""));
      return "Erased";
    }  

    /**
     * Update Selection for $user_id
     *
     * @access public
     * @param int $user_id
     * @param array $attributes
     * @return void
     */

    function update($user_id, $attributes = array())
    {

        // Update
        if ($this->get_by_user_id($user_id))
        {
            $this->db->where('user_id', $user_id);
            $this->db->update('selections', $attributes);
        }
        // Insert
        else
        {
            $attributes['user_id'] = $user_id;
            $this->db->insert('selections', $attributes);
        }

    }



	/**
	 * Delete Selection by user_id Result
	 *
	 * @access public
	 * @param int $user_id
	 * @return void
	 */
    function delete($user_id)
    {
    	$this->db->delete('selections', array('user_id' => $user_id));
      
    }

}
